package com.lianbang.staff.vm

import androidx.lifecycle.LiveData
import cn.ubest.mvvmcore.base.BaseViewModel
import cn.ubest.mvvmcore.bean.ResultState
import cn.ubest.mvvmcore.utils.ToastUtils
import com.lianbang.staff.bean.CheckCodeBean
import com.lianbang.staff.bean.UserBean
import com.lianbang.staff.model.UserModel
import com.lianbang.staff.util.LoginUtils
import kotlinx.coroutines.delay

/**
 * Created by maoqi on 2021/1/13.
 */
class UserVM : BaseViewModel<UserModel>() {

    fun login(account: String, pwd: String): LiveData<ResultState<UserBean>> {
        return requestWithLife {
            val userBean = mModel.login(account, pwd).resolveWrapper()
            LoginUtils.mToken = userBean.token
            LoginUtils.loginSucc(userBean)
            userBean
        }
    }

    fun changePassword(oldPwd: String, newPwd: String, confirmPwd: String) {
        requestWithLife(getHandler1({
            mModel.changePassword(oldPwd, newPwd, confirmPwd).resolveWrapper()
        }) {
            ToastUtils.toast("修改成功")
            finishActivity()
        })
    }

    //找回密码
    fun findPassword(
        mobile: String,
        auth_token: String,
        newPwd: String,
        confirmPwd: String
    ): LiveData<ResultState<Boolean>> {
        return requestWithLife {
            mModel.findPassword(mobile, auth_token, newPwd, confirmPwd).resolveWrapper()
        }
    }

    //找回密码
    fun checkFindPasswordCode(
        mobile: String,
        code: String
    ): LiveData<ResultState<CheckCodeBean>> {
        return requestWithLife {
            mModel.checkFindPasswordCode(mobile, code).resolveWrapper()
        }
    }

    //发送验证码
    fun sendCode(
        mobile: String,
        sms_auth: String,
        type: String
    ): LiveData<ResultState<Boolean>> {
        return requestWithLife {
            delay(1000)
            true
        }
    }
}